<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
$return['mid']=$_SESSION['uid'];
$return['show_setmobile']=cfc('core')->check_setmobile(1);
$return['show_setmember']=cfc('core')->check_setmember(1);
if($_RQ['op']=='main'){
	if($_RQ['goodsid']){
		$login_burl='jy_lianfeeshop/goods.detail//id='.$_RQ['goodsid'];
		$this->session('login_burl',$login_burl);
	}
	$return=model('jy_weishop/get_order')->getdata($_RQ);
	if($_SESSION['openid'.$merchid] || $_SESSION['onemerchid']){
		$return['onemerchid']=$_SESSION['onemerchid']?:$merchid;
	}
	$payset=uni_setting('pay');
	$credit3_rate=$payset['credit3_rate']?:1;
	$return['credit3_rate']=$credit3_rate;
	foreach($return['order']['list'] as &$item){
		$item['credit3']=price($item['price']*$credit3_rate,'int');
		$item['api_data']=json_decode($item['api_data'],true);
		if($item['api_data']['dingpei']){
			$return['post']['is_dingpei']=1;
			break;
		}
	}
	$return['post']['credit3']=price($return['post']['endprice']*$credit3_rate,'int');
	$return['post']['no_dispatch_credit3']=price($return['post']['no_dispatch_price']*$credit3_rate,'int');
	if($return['order']['merchid']){
		$merchset=plugin('jy_merch')->plugin_setting('setting');
		if($merchset['ordertype']==1){
			$return['post']['buytype']=4;
			$return['peerpay']=false;
		}
	}
	//返回链接
	$return['forward_url']=base64_encode('jy_lianfeeshop/order.list//fromcreate=1');
	set_html('title','确认订单');
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$topaytype=$post['topaytype'];
	//整理订单数据
	$new_order=model('jy_weishop/get_order')->new_order($post);
	$new_order['plugin_name']='jy_lianfeeshop';
	if(!$new_order['merchid'] && $_SESSION['onemerchid']){
		$new_order['api_data']['dhmerchid']=$_SESSION['onemerchid'];
	}
	if($topaytype){
		$payset=uni_setting('pay');
		$credit3_rate=$payset['credit3_rate']?:1;
		$credit3=price($new_order['price']*$credit3_rate,'int');
		$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit5','credit6','mobile','avatar','nickname'));
		if($member['credit3']<$credit3){
			exi('当前兑换券不足！不能兑换！','error');
		}
	}
	$order_id=model('jy_weishop/order')->create($new_order);
	$success_url='jy_lianfeeshop/order.list/';
	if($topaytype){
		cfc('core')->member_credit($_SESSION['uid'],'credit3',$credit3,false,'jy_lianfeeshop','兑换商品');
		$params=array(
			'paytype'=>'credit3',
		);
		model('jy_weishop/order')->order_pay($order_id,$params);
	}else{
		$params=array(
			'id'=>$order_id,
			'title'=>'购买商品',
			'fee'=>$post['buytype']==4?$new_order['dispatchprice']:$new_order['price'],
			'oldfee'=>$new_order['oldfee'],
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_lianfeeshop/order.create/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$success_url?:'jy_lianfeeshop/order.list/',
			'error_url'=>$error_url?:'jy_lianfeeshop/order.list/',
		);
		//虚拟设置
		$plugin_xuni=model('jy_weishop/activity')->plugin_xuni($new_order);
		if(count($plugin_xuni)){
			$params['plugin_xuni']=$plugin_xuni;
		}
		if($new_order['merchid']){
			$merchset=plugin('jy_merch')->plugin_setting('setting');
		}
		if($merchset['isone_credit'] && $new_order['merchid']){
			$params['merchid']=$new_order['merchid'];
		}
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_weishop_order',array('pay_id'=>$payid),array('id'=>$order_id));
		if($post['peerpay']){
			$peerpay_setting=plugin('jy_weishop')->plugin_setting('peerpay');
			if($peerpay_setting && $peerpay_setting['open']){
				exi('','','jy_weishop/order.peerpay//orderid='.$order_id);
			}
		}
	}
	$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>$success_url,
			'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_lianfeeshop/order.list/');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_lianfeeshop/order.list/');
	}
	if($order['buytype']!=4 && $order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_lianfeeshop/order.list/');
	}
	if($order['buytype']==4 && $order['dispatchprice']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['dispatchprice'].'|'.$payresult['fee'],'error','jy_lianfeeshop/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	model('jy_weishop/order')->order_pay($order['id'],$params);
	exi('提交成功!');
}

?>